<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>面试题二：求出现的次数</title>
  </head>

  <body>

  </body>
  <script>
    /* 
  ['red', 'blue', 'red', 'green', 'yellow', 'red', 'green']求'red'出现的次数和位置
*/
    var count = 0;
    var pos = [];

    var findCount = function (arr) {
      for (let i = 0; i < arr.length; i++) {
        if (arr[i] === "red") {
          count++;
          pos.push(i);
        }
      }
      return {
        count: count,
        pos: pos
      }
    }
    var obj = findCount(['red', 'blue', 'red', 'green', 'yellow', 'red', 'green']);
    console.log("['red', 'blue', 'red', 'green', 'yellow', 'red', 'green']中'red'出现的次数为" + obj.count + "和位置为" + obj.pos
      .toString());
  </script>

</html>